<?php


namespace App\Api\Controllers;


use App\Api\Manages\CommonController;
use App\Api\Repositories\AfterSalesRepositorie;
use App\Api\Repositories\CollectionRepositorie;
use App\Api\Repositories\GoodsCartRepositories;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;

class ShopingCartController extends CommonController
{
    //添加购物车
    public function addCart(Request $request)
    {
        //验证并获得加入购物车商品的ID
        $validat = $request->validate([
            'gid' => ['required'],
            'uid' => ['required']
        ]);
        //验证是否已经有购物车数据
        $have = GoodsCartRepositories::haveData($validat['gid'],$validat['uid']);
        if($have){
            return  JsonService::fail('该商品已在您的购物车中','',config('code.HTTP_FAIL'));
        }else{
            //获取商品详情
            $add = GoodsCartRepositories::addGoodsCart((int)$validat['gid'],(int)$validat['uid']);
            if($add){
                return JsonService::success('添加购物车成功','','1000');
            }else{
                return JsonService::fail('添加购物车失败','',config('code.HTTP_FAIL'));
            }
        }
    }

    //商品售后
    public function afterSales(Request $request)
    {
        //验证并获得加入购物车商品的ID
        $validat = $request->validate([
            'gid' => ['required'],
            'uid' => ['required']
        ]);
        //验证是否存在售后
        $aftersales = AfterSalesRepositorie::getSales((int)$validat['gid'],(int)$validat['uid']);
        if(empty($aftersales)){
            return JsonService::fail('暂无售后数据','',config('code.HTTP_NULL_DATA'));
        }else{
            return JsonService::success('查询成功',$aftersales,config('code.HTTP_OK'));
        }
    }

    //收藏
    public function collection(Request $request)
    {
        //验证并获得加入购物车商品的ID
        $validat = $request->validate([
            'gid' => ['required'],
            'uid' => ['required']
        ]);
        //验证是否收藏
        $collection = CollectionRepositorie::isCollection((int)$validat['gid'],(int)$validat['uid']);
        if($collection){
            //收藏状态
            $status = CollectionRepositorie::isStatus((int)$validat['gid'],(int)$validat['uid']);
            if($status){
                CollectionRepositorie::cancellationCollection((int)$validat['gid'],(int)$validat['uid']);
                return JsonService::success('您已取消收藏','','1007');
            }else{
                CollectionRepositorie::successCollection((int)$validat['gid'],(int)$validat['uid']);
                return JsonService::success('收藏成功','','1000');
            }
        }else{
            $add = CollectionRepositorie::addCollection((int)$validat['gid'],(int)$validat['uid']);
            if($add){
                return JsonService::success('收藏成功','','1000');
            }else{
                return JsonService::fail('收藏失败','','1007');
            }
        }
    }
}